#include<iostream>
#include<algorithm>
using namespace std;
const int N = 110, M = 210;
int n, m;
struct Edge
{
    int a, b, w;
    bool operator<(const Edge& t) const
    {
        return w < t.w;
    }
}edge[M];
int p[N];
int find(int x)
{
    if (p[x] != x) p[x] = find(p[x]);
    return p[x];
}
int main()
{
    cin >> n >> m;
    for (int i = 0; i < m; i++)
    {
        int a, b, w;
        cin >> a >> b >> w;
        edge[i] = { a,b,w };
    }
    for (int i = 1; i <= n; i++) p[i] = i;
    sort(edge, edge + m);
    int res = 0;
    for (int i = 0; i < m; i++)
    {
        int a = find(edge[i].a), b = find(edge[i].b), w = edge[i].w;
        if (a != b) p[a] = b;
        else res += w;
    }
    cout << res << endl;
    return 0;
}